package com.dingup.provider.aop.lessonInfoLog;

import com.dingup.api.Util.LoggerUtil;
import com.dingup.api.enums.log.LessonInfoLogEnum;
import com.dingup.api.enums.log.LogModelEnum;
import com.dingup.api.enums.log.LogUpdateTypeEnum;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.api.model.lesson.LessonInfo;
import com.dingup.api.model.lesson.LessonSubjectInfo;
import com.dingup.provider.mapper.LessonInfoMapper;
import com.dingup.provider.mapper.LessonSubjectInfoMapper;
import com.dingup.provider.service.log.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/8/29.
 */
@Component
@Aspect
public class LessonInfoLogAop {

    @Autowired
    LessonInfoMapper lessonInfoMapper;
    @Autowired
    LessonSubjectInfoMapper lessonSubjectInfoMapper;
    @Autowired
    SysLogService sysLogService;

    @AfterReturning(value = "execution(* com.dingup.provider.service.lesson.impl.LessonServiceImpl.saveLessonInfo(..))",returning = "returnValue")
    public void afterSaveLessonLog(JoinPoint joinPoint,LessonInfo returnValue) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            LessonInfo lessonInfo = (LessonInfo) obj[0];
            sysLogService.saveLog(lessonInfo.getCreateUserId(), LogUpdateTypeEnum.ADD, "创建了该课程", returnValue.getId(), LogModelEnum.LESSONINFO);
        }
    }

    @Before("execution(* com.dingup.provider.mapper.LessonInfoMapper.updateByPrimaryKeySelective(..))")
    public void afterUpdateLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            LessonInfo lessonInfo = (LessonInfo) obj[0];
            String operaUserId = lessonInfo.getOperaUserId();
            LessonInfo older = lessonInfoMapper.selectByPrimaryKey(lessonInfo.getId());
            try {
                List<String> content = LoggerUtil.getUpdateAttribute(older, lessonInfo, LessonInfo.class);
                if (!CollectionUtils.isEmpty(content)) {
                    String contentString = content.stream().collect(Collectors.joining("、 "));
                    sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE, contentString, lessonInfo.getId(), LogModelEnum.LESSONINFO);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Before("execution(* com.dingup.provider.service.lesson.impl.LessonSubjectInfoServiceImpl.needInsert(..))")
    public void saveLessonSubjectLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            List<LessonSubjectInfo> lessonSubjectInfo = (List<LessonSubjectInfo>) obj[0];
            String operaUserId = (String)obj[1];
            Integer lessonId = (Integer) obj[2];
            StringBuffer content = new StringBuffer("课时 ");
            for (LessonSubjectInfo l: lessonSubjectInfo) {
                content.append(l.getNameCh() + " ");
            }
            sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_ADD, content.toString(), lessonId, LogModelEnum.LESSONINFO);
        }
    }

    @Before("execution(* com.dingup.provider.service.lesson.impl.LessonSubjectInfoServiceImpl.needDelete(..))")
    public void deleteLessonSubjectInfoLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            List<LessonSubjectInfo> lessonSubjectInfo = (List<LessonSubjectInfo>) obj[0];
            String operaUserId = (String)obj[1];
            Integer lessonId = (Integer) obj[2];
            StringBuffer content = new StringBuffer("课时 ");
            for (LessonSubjectInfo l: lessonSubjectInfo) {
                content.append(l.getNameCh() + " ");
            }
            sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_DELETE, content.toString(), lessonId, LogModelEnum.LESSONINFO);
        }
    }
    @Before("execution(* com.dingup.provider.service.lesson.impl.LessonSubjectInfoServiceImpl.needUpdate(..))")
    public void updateLessonSubjectInfoLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            List<LessonSubjectInfo> lessonSubjectInfo = (List<LessonSubjectInfo>) obj[0];
            String operaUserId = (String)obj[1];
            Integer lessonId = (Integer) obj[2];
            StringBuffer content = new StringBuffer("课时 ");
            for (LessonSubjectInfo l: lessonSubjectInfo) {
                content.append(l.getNameCh() + " ");
            }
            sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE, content.toString(), lessonId, LogModelEnum.LESSONINFO);
        }
    }

    @Before("execution(* com.dingup.provider.service.lesson.impl.LessonServiceImpl.updateLessonIfOnShelf(..))")
    public void ifOnShelfSubjectInfoLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            Integer lessonId = (Integer) obj[0];
            Boolean ifOnShelf = (Boolean)obj[1];
            String operaUserId = (String)obj[2];
            StringBuffer content = new StringBuffer("");
            LogUpdateTypeEnum status = null;
            if (ifOnShelf){
                content.append("上架了该课程");
                status = LogUpdateTypeEnum.UPDATE_LESSON_ON_SHELF;
            }else{
                content.append("下架了该课程");
                status = LogUpdateTypeEnum.UPDATE_LESSON_LOWER_FRAME;
            }
            sysLogService.saveLog(operaUserId, status, content.toString(), lessonId, LogModelEnum.LESSONINFO);
        }
    }

}
